package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.home.model.bean.FightGroupShare;
import com.meitianhui.convenitenbuy.module.order.ui.activity.OrderActivity;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.ShareDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/*
 Created by Administrator on 2017/11/18.
 */

public class FightGroupShareActivity extends BaseToolbarActivity {

    private String mJson;
    private String mType;
    private TextView mTvContent;
    private FightGroupShare mFightGroupShare;
    private UMImage mUmImage;
    private ShareDialog mShareDialog;

    @Override
    protected int setView() {
        return R.layout.activity_fight_group_share;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        isEnable(false);
        RxUtils.bindEvents(getViewById(R.id.tv_share), this);
        RxUtils.bindEvents(getViewById(R.id.tv_order), this);
        mTvContent = getViewById(R.id.tv_content);
        mJson = getIntent().getStringExtra("json");
        mType = getIntent().getStringExtra("type");
        if (!TextUtils.isEmpty(mJson)) {
            mFightGroupShare = new Gson().fromJson(mJson, FightGroupShare.class);
        }
        if ("join_fight".equals(mType)) {
            setTbTitle("我要参团");
            String stv = mTvContent.getText().toString();
            stv = stv.replaceAll("发布成功", "参团成功");
            mTvContent.setText(stv);
        } else {
            setTbTitle("我要开团");
        }

    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.tv_share:   //分享
                share();
                break;
            case R.id.tv_order:  //订单
                Intent intent = new Intent(FightGroupShareActivity.this, OrderActivity.class);
                intent.putExtra("order_type", 2);
                startActivity(intent);
                finish();
                break;

        }
    }


    public void share() {
        if (mUmImage == null)
            mUmImage = new UMImage(this, R.mipmap.logo);
        mShareDialog = new ShareDialog(this, onShareClickListener, Constants.TYPE_NO_SHOW_WXMINI_SHARE);
        mShareDialog.show();
    }

    ShareDialog.OnShareClickListener onShareClickListener = new ShareDialog.OnShareClickListener() {

        @Override
        public void FriendCircle(View v) {
            UMWeb web = new UMWeb(mFightGroupShare.getUrl());
            web.setTitle(mFightGroupShare.getTitle());//标题
            web.setThumb(mUmImage);  //缩略图
            web.setDescription(mFightGroupShare.getDesc());//描述
            new ShareAction(FightGroupShareActivity.this)
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                    .setCallback(shareListener)
                    .withMedia(web)
                    .share();
            mShareDialog.dismiss();

        }

        @Override
        public void WeChatFriends(View v) {
            UMWeb web = new UMWeb(mFightGroupShare.getUrl());
            web.setTitle(mFightGroupShare.getTitle());//标题
            web.setThumb(mUmImage);  //缩略图
            web.setDescription(mFightGroupShare.getDesc());//描述
            new ShareAction(FightGroupShareActivity.this)
                    .setPlatform(SHARE_MEDIA.WEIXIN)
                    .setCallback(shareListener)
                    .withMedia(web)
                    .share();
        }

        @Override
        public void QQFriends(View v) {
            UMWeb web = new UMWeb(mFightGroupShare.getUrl());
            web.setTitle(mFightGroupShare.getTitle());//标题
            web.setThumb(mUmImage);  //缩略图
            web.setDescription(mFightGroupShare.getDesc());//描述
            new ShareAction(FightGroupShareActivity.this)
                    .setPlatform(SHARE_MEDIA.QQ)
                    .setCallback(shareListener)
                    .withMedia(web)
                    .share();

        }

        @Override
        public void QQZone(View v) {
            UMWeb web = new UMWeb(mFightGroupShare.getUrl());
            web.setTitle(mFightGroupShare.getTitle());//标题
            web.setThumb(mUmImage);  //缩略图
            web.setDescription(mFightGroupShare.getDesc());//描述
            new ShareAction(FightGroupShareActivity.this)
                    .setPlatform(SHARE_MEDIA.QZONE)
                    .setCallback(shareListener)
                    .withMedia(web)
                    .share();

        }

        @Override
        public void WxMini(View v) {

        }
    };
    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            TipUtils.showToast(App.getInstance(), "成功了");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            TipUtils.showToast(App.getInstance(), "失败" + t.getMessage());
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            TipUtils.showToast(App.getInstance(), "取消了");
        }
    };

    @Override
    protected void onRefreshData() {

    }
}
